package org.itboys.portal.service.manager;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.time.TimeUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.portal.dto.mobile.MobiPortalInfoResult;
import org.itboys.portal.dto.mobile.MobiPortalItemResult;
import org.itboys.portal.entity.PortalInfo;
import org.itboys.portal.entity.PortalItem;
import org.itboys.portal.service.PortalInfoService;
import org.itboys.portal.service.PortalItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/** 
 * @author 作者 huml 
 * @version 创建时间：2016-3-30 上午9:59:37 
 * 手机端资讯业务 
 */
@Service
public class MobiPortalInfoManager {

	@Autowired
	private PortalInfoService portalInfoService;
	@Autowired
	private PortalItemService portalItemService;
	@Autowired
	private ResourceHolder resourceHolder;
	
	public static final String MOBILE_ITEM_CODE = "mobile";
	/**
	 * 手机端资讯栏目列表
	 * 需要在后台新建一个code为mobile的父栏目,然后在该父栏目下新建子栏目,新建的子栏目即为这里返回给手机端的栏目
	 * @return
	 */
	public List<MobiPortalItemResult> itemList(){
		List<PortalItem> items = portalItemService.getChildItems(MOBILE_ITEM_CODE);//获得手机端所有的资讯栏目
		if(items == null || items.isEmpty()){
			return null;//暂时没有栏目
		}
		List<MobiPortalItemResult> results = Lists.newArrayListWithExpectedSize(items.size());
		for (PortalItem item : items) {
			MobiPortalItemResult result = new MobiPortalItemResult();
			result.setId(item.getId());
			result.setName(item.getName());
			result.setImage(StringUtils.isNotBlank(item.getImage()) ? resourceHolder.getStringValue("imgRoot")+item.getImage() 
					: StringUtils.EMPTY);
			
			results.add(result);
		}
		return results;
	}
	/**
	 * 分页获取某一栏目下的信息列表
	 * @param itemId
	 * @param request
	 * @return
	 */
	public List<MobiPortalInfoResult> getInfosByItemId(Long itemId, HttpServletRequest request){
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(1);
		param.put("itemId", itemId);
		List<PortalInfo> list = portalInfoService.pageQuery2(request, param).getData();
		List<MobiPortalInfoResult> results = Lists.newArrayListWithExpectedSize(list.size());
		for (PortalInfo portalInfo : list) {
			MobiPortalInfoResult result = new MobiPortalInfoResult();
			result.setId(portalInfo.getId());
			result.setTitle(portalInfo.getTitle());
			result.setSummary(portalInfo.getSummary());
			result.setTime(TimeUtils.format(portalInfo.getCreateTime(), CommonConstants.DATE.FORMAT_DEFAULT));
			result.setImage(StringUtils.isNotBlank(portalInfo.getImage()) ? resourceHolder.getStringValue("imgRoot")+portalInfo.getImage() 
					: StringUtils.EMPTY);
			result.setDetailUrl(resourceHolder.getStringValue("adminRoot")+"/mobile/core/portal/info/html?id="+portalInfo.getId());
			result.setCommentCount(portalInfo.getCommentCount() == null ? 0L : portalInfo.getCommentCount());
			result.setGoodCount(portalInfo.getGoodCount() == null ? 0L : portalInfo.getGoodCount());
			results.add(result);
		}
		return results;
	}
	/**
	 * 根据栏目code获取资讯列表
	 * @param code
	 * @param request
	 * @return
	 */
	public List<MobiPortalInfoResult> getInfosByItemCode(String code, HttpServletRequest request){
		PortalItem item = portalItemService.findByCode(code);
		if(item != null){
			return getInfosByItemId(item.getId(), request);
		}
		return null;
	}
	
	/**
	 * 评论数加一
	 * @param infoId
	 */
	public void addCommentCount(Long infoId){
		PortalInfo info = portalInfoService.getById(infoId);
		info.setCommentCount(info.getCommentCount() == null ? 1L : info.getCommentCount() + 1);
		portalInfoService.update(info);
	}
	
	/**
	 * 评论数减一
	 * @param infoId
	 */
	public void subCommentCount(Long infoId){
		PortalInfo info = portalInfoService.getById(infoId);
		info.setCommentCount(info.getCommentCount() <= 0L ? 0L : info.getCommentCount() - 1);
		portalInfoService.update(info);
	}
	/**
	 * 点赞数加一
	 * @param infoId
	 */
	public void addGoodCount(Long infoId){
		PortalInfo info = portalInfoService.getById(infoId);
		info.setGoodCount(info.getGoodCount() == null ? 1L : info.getGoodCount() + 1);
		portalInfoService.update(info);
	}
	
	/**
	 * 点赞数减一
	 * @param infoId
	 */
	public void subGoodCount(Long infoId){
		PortalInfo info = portalInfoService.getById(infoId);
		info.setGoodCount(info.getGoodCount() <= 0L ? 0L : info.getGoodCount() - 1);
		portalInfoService.update(info);
	}
	
}
